import request from '@/utils/request'

// 获取客服列表
export function getInCustomerServiceList(params: {
    page_no: number
    page_size: number
    nickname?: string
}) {
    return request.get({
        url: '/kefu.inkefu/lists',
        params
    })
}

// 获取客服详情
export function getInCustomerServiceDetail(id: string | number) {
    return request.get({
        url: '/kefu.inkefu/kefuDetails',
        params: { id }
    })
}

// 添加客服
export function addInCustomerService(params: {
    nickname: string
    avatar: File
}) {
    return request.post({
        url: '/kefu.inkefu/add',
        params
    })
}

// 编辑客服
export function editInCustomerService(file: File, nickname: string, open_kfid?: string) {
    const formData = new FormData()
    formData.append('file', file)
    formData.append('nickname', nickname)
    if (open_kfid) {
        formData.append('open_kfid', open_kfid)
    }
    
    return request.post({
        url: '/kefu.inkefu/edit',
        data: formData,
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })
}

// 上传客服头像
export function uploadInServiceAvatar(file: File, nickname?: string) {
    const formData = new FormData()
    formData.append('file', file)
    if (nickname) {
        formData.append('nickname', nickname)
    }
    
    return request.post({
        url: '/kefu.inkefu/add',
        data: formData,
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })
}

// 获取客服聊天用户列表
export function getInCustomerChatUsers(id: string | number, params: {
    page_no: number
    page_size: number
    keyword?: string
}) {
    return request.get({
        url: '/kefu.inkefu/chatList',
        params: {
            id: id,
            ...params
        }
    })
}

// 获取客服与用户的聊天记录
export function getInChatHistory(params: {
    kefu_id: string | number
    customer_id: string | number
    page_no: number
    page_size: number
}) {
    return request.get({
        url: '/kefu.inkefu/historyList',
        params
    })
}

// 发送消息
export function sendInChatMessage(params: {
    kefu_id: string | number
    customer_id: string | number
    content: string
    type?: string // text, image, file 等
}) {
    return request.post({
        url: '/kefu.inkefu/sendMsg',
        params
    })
}

// 上传图片
export function uploadInChatImage(file: File, params: {
    kefu_id: string | number
    customer_id: string | number
}) {
    const formData = new FormData()
    formData.append('file', file)
    formData.append('kefu_id', params.kefu_id.toString())
    formData.append('customer_id', params.customer_id.toString())
    
    return request.post({
        url: '/kefu.inkefu/sendMsg',
        data: formData,
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })
}

// 获取客服会话列表
export function getInCustomerServiceConversations(id: string | number, params: {
    pageNum: number
    pageSize: number
    startTime?: string
    endTime?: string
}) {
    return request.get({
        url: '/customer-service/conversations',
        params: {
            serviceId: id,
            ...params
        }
    })
}

// 获取客服分享链接
export function getInCustomerServiceLink(kefu_id: string | number) {
    return request.get({
        url: '/kefu.inkefu/createKefuUrl',
        params: { kefu_id }
    })
}

// 设置客服欢迎语
export function setInWelcomeMessage(params: {
    kefu_id: string | number
    welcome_text: string
}) {
    return request.post({
        url: '/kefu.inkefu/saveWelcome',
        params
    })
}

// 获取客服欢迎语
export function getInWelcomeMessage(kefu_id: string | number) {
    return request.get({
        url: '/kefu.inkefu/getWelcome',
        params: { kefu_id }
    })
}

// 设置客服自动回复开关
export function setInAutoReplyStatus(params: {
    kefu_id: string | number
    is_open_keywords: boolean
}) {
    return request.post({
        url: '/kefu.inkefu/changeKeywordsStatus',
        params
    })
}


export function setInAutoReplyStatus2(params: {
    kefu_id: string | number
    is_open_auto_reply: boolean
}) {
    return request.post({
        url: '/kefu.inkefu/autoReplyStatus',
        params
    })
}


export function getInKefuWebLink(params: {
    kefu_id: string | number
}) {
    return request.post({
        url: '/kefu.inkefu/createWebUrl',
        params
    })
}


export function getInServicerList(params: {
    kefu_id: string | number
}) {
    return request.post({
        url: '/kefu.inkefu/getServicerList',
        params
    })
}

// 获取关键词回复列表
export function getInKeywordReplyList(kefu_id: string | number, params?: {
    page_no?: number
    page_size?: number
}) {
    return request.get({
        url: '/kefu.inkefu/keywordsList',
        params: {
            kefu_id,
            ...params
        }
    })
}

// 新增关键词回复
export function addInKeywordReply(params: {
    kefu_id: string | number
    keywords: string
    type: 1 | 2
    content: string
}) {
    return request.post({
        url: '/kefu.inkefu/addKeywords',
        params
    })
}

// 编辑关键词回复
export function updateInKeywordReply(params: {
    id: string | number
    kefu_id: string | number
    keyword: string
    type: 'text' | 'image'
    content: string
}) {
    return request.post({
        url: '/kefu.inkefu/updateKeyword',
        params
    })
}

// 删除关键词回复
export function deleteInKeywordReply(id: string | number) {
    return request.post({
        url: '/kefu.inkefu/delKeywords',
        params: { id }
    })
}

// 上传关键词回复图片
export function uploadInKeywordImage(file: File, kefu_id: string | number,keyword:string,type:number) {
    const formData = new FormData()
    formData.append('file', file)
    formData.append('kefu_id', kefu_id.toString())
    formData.append('keywords', keyword)
    formData.append('type', type.toString())

    return request.post({
        url: '/kefu.inkefu/addKeywords',
        data: formData,
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })
}

// 删除客服
export function deleteInCustomerService(id: string | number) {
    return request.post({
        url: '/kefu.inkefu/delKefu',
        params: { id }
    })
}

// 获取自动回复列表
export function getInAutoReplyList(kefuId: string | number) {
  return request.get({
    url: `/kefu.inkefu/autoReplyList?kefu_id=${kefuId}`
  })
}

// 获取客服公众号二维码
export function getInKefuWxgzh(kefuId: string | number) {
    return request.get({
      url: `/kefu.wxgzh/createQrcodeByForever?kefu_id=${kefuId}`
    })
  }

// 新增自动回复
export function addInAutoReply(data: {
  kefu_id: string | number
  type: 1 | 2
  content: string
}) {
  return request.post({
    url: '/kefu.inkefu/addAutoReply',
    method: 'post',
    data
  })
}

// 更新自动回复
export function updateInAutoReply(data: {
  id: string | number
  kefu_id: string | number
  type: 1 | 2
  content: string
}) {
  return request.post({
    url: '/customer-service/auto-reply/update',
    method: 'post',
    data
  })
}

// 删除自动回复
export function deleteInAutoReply(id: string | number) {
  return request.get({
    url: `/kefu.inkefu/delAutoReply?id=${id}`,
    method: 'post'
  })
}

// 上传自动回复图片
export function uploadInAutoReplyImage(file: File, kefuId: string | number,type:number) {
  const formData = new FormData()
  formData.append('file', file)
  formData.append('kefu_id', kefuId.toString())
  formData.append('type', type.toString());
  
  return request.post({
    url: '/kefu.inkefu/addAutoReply',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 获取广告列表
export function getAdvertisementList() {
  return request.get({
    url: '/ads.tencentAdvertisingAdGroup/allLists'
  })
}

// 获取创意列表
export function getCreativeList() {
  return request.get({
    url: '/ads.tencentAdvertisingAdGroup/allCreativeLists'
  })
}

// 生成服务链接
export function generateInServiceLink(params: {
  kefu_id: string | number
  type: string
  ad_id?: string | number
  creative_id?: string | number
}) {
  return request.post({
    url: '/kefu.inkefu/createKefuUrl',
    params
  })
} 